package cn.fl.capital.service.impl;

import java.util.List;

import cn.fl.export.api.ICapFundExportApiService;
import cn.fl.export.condition.CapDailyRepaymentExportCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.fl.capital.condition.CapDailyRepaymentQueryCondition;
import cn.fl.capital.dto.CapDailyRepaymentDTO;
import cn.fl.capital.service.ICapDailyRepaymentApiService;
import cn.fl.capital.service.ICapDailyRepaymentService;
import cn.fl.capital.transform.CapDailyRepaymentVOTransform;
import cn.fl.capital.vo.CapDailyRepaymentVO;
import cn.fl.framework.base.context.CurrentThreadContext;
/**
 * @Description: 每日还款数据信息  客户端服务层接口实现
 */
@Service
public class CapDailyRepaymentServiceImpl implements ICapDailyRepaymentService{
 
	/**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(CapDailyRepaymentServiceImpl.class);
    
    /**
	 * API 暴露服务
	 */
	@Autowired
	private ICapDailyRepaymentApiService capDailyRepaymentApiService;
	@Autowired
	private ICapFundExportApiService capFundExportApiService;
    
    @Override
	public Pager searchListPage(Pager page) throws BaseException{
        CapDailyRepaymentQueryCondition condition=null;
        if(null == page) {
            page = new Pager();
            page.setCondition(new CapDailyRepaymentQueryCondition());
        }
		condition=(CapDailyRepaymentQueryCondition)page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		page=this.capDailyRepaymentApiService.searchCapDailyRepaymentListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
            List<CapDailyRepaymentVO> voList=CapDailyRepaymentVOTransform.toVOList((List<CapDailyRepaymentDTO>)page.getResultList());
            //this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
	}
    /**
	 * 批量导出分解单
	 *
	 * @param condition
	 * @return
	 * @throws BaseException
	 */
	@Override
	public DataResultVO exportRepaymentList(CapDailyRepaymentExportCondition condition) throws BaseException {
	    if(null == condition){
	        condition = new CapDailyRepaymentExportCondition();
        }
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
        try {
            DataResultDTO resultDTO=this.capFundExportApiService.exportRepaymentList(condition);
            if(null==resultDTO){
                return new DataResultVO(BaseConstant.IS_YESNO_NO,"导出失败");
            }else{
                return new DataResultVO(Integer.parseInt(resultDTO.getCode()),resultDTO.getInfo(),resultDTO.getData());
            }
        } catch (BaseException e) {
            BaseLogUtils.error(logger,"exportDecomposeList","批量导出每日还款数据失败");
			BaseLogUtils.error(logger, "-", "异常", e);
        } catch (NumberFormatException e) {
            BaseLogUtils.error(logger,"exportDecomposeList","批量导出每日还款数据失败");
			BaseLogUtils.error(logger, "-", "异常", e);
        }
        return new DataResultVO(BaseConstant.IS_YESNO_NO,"导出失败");
    }
}
